package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Booking;
import com.java.second_goods.entity.bo.BookingRequestDTO;
import com.java.second_goods.service.BookingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单信息
 */
@RestController
@RequestMapping("/api/booking")
@Tag(name = "订单管理", description = "订单相关接口")
public class BookingController extends BaseController {

    @Autowired
    private BookingService bookingService;

    /**
     * 获取订单列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取订单列表", description = "获取订单列表")
    public AjaxResult list(Booking booking) {
        startPage(1, 10);
        List<Booking> list = bookingService.selectBookingList(booking);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取订单详细信息", description = "根据订单ID获取订单详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(bookingService.selectBookingById(id));
    }

    /**
     * 新增订单
     */
    @PostMapping
    @Operation(summary = "新增订单", description = "新增订单")
    public AjaxResult add(@RequestBody Booking booking) {
        return toAjax(bookingService.insertBooking(booking));
    }

    /**
     * 修改订单
     */
    @PutMapping
    @Operation(summary = "修改订单", description = "修改订单")
    public AjaxResult edit(@RequestBody Booking booking) {
        return toAjax(bookingService.updateBooking(booking));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除订单", description = "删除订单")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bookingService.deleteBookingByIds(ids));
    }

    /**
     * 根据用户ID查询订单列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID查询订单列表", description = "根据用户ID查询订单列表")
    public AjaxResult getBookingsByUser(@PathVariable("userId") Integer userId) {
        List<Booking> bookings = bookingService.selectBookingsByUserId(userId);
        return success(bookings);
    }



    /**
     * 根据状态查询订单列表
     */
    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态查询订单列表", description = "根据状态查询订单列表")
    public AjaxResult getBookingsByStatus(@PathVariable("status") String status) {
        List<Booking> bookings = bookingService.selectBookingsByStatus(status);
        return success(bookings);
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/status/{bookingId}/{status}")
    @Operation(summary = "更新订单状态", description = "更新订单状态")
    public AjaxResult updateStatus(@PathVariable("bookingId") String bookingId,
                                   @PathVariable("status") String status) {
        return toAjax(bookingService.updateBookingStatus(bookingId, status));
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{bookingId}")
    @Operation(summary = "取消订单", description = "取消订单")
    public AjaxResult cancel(@PathVariable("bookingId") String bookingId) {
        return toAjax(bookingService.cancelBooking(bookingId));
    }

    /**
     * 检查是否有重复预约
     */
    @GetMapping("/check")
    @Operation(summary = "检查是否有重复预约", description = "检查是否有重复预约")
    public AjaxResult checkDuplicateBooking(
            @RequestParam("chefId") Integer goodsId,
            @RequestParam("bookingDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date bookingDate,
            @RequestParam("bookingTime") @DateTimeFormat(pattern = "HH:mm:ss") Date bookingTime) {
        boolean isDuplicate = bookingService.checkDuplicateBooking(goodsId, bookingDate, bookingTime);
        return success(!isDuplicate);
    }

    /**
     * 创建订单
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "创建订单")
    public AjaxResult createBooking(@RequestBody BookingRequestDTO requestDTO) {
            Booking booking = requestDTO.toBooking();
            Booking result = bookingService.createBooking(booking);
        if (result != null) {
            return success("新增成功", result);
        } else {
            return error("失败");
        }
    }
}